<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .upload {
            position: relative;
            width: 100px;
            height: 100px;
        }
        
        .upload .title {
            width: 100px;
            height: 100px;
            border: 1px dashed #ddd;
            line-height: 100px;
            text-align: center;
        }
        
        .upload #input {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            opacity: 0;
        }
    </style>
</head>

<body>
    <!-- <input type="file"> 单选文件上传 -->
    <!-- <input type="file" multiple> 多个文件 -->
    <div class="upload">
        <div class="title">+</div>
        <input type="file" id="input" multiple accept=".jpg,.png">
    </div>
</body>
<script src="../node_modules/axios/dist/axios.js"></script>
<script>
    let url = "http://tt.linweiqin.com/api/tt/aliossUpload";
    document.getElementById("input").addEventListener("change", function(e) {
        console.log(e);
        console.log(e.target.files);
        Array.from(e.target.files).forEach((f) => {
            // 1. 构造请求的参数
            // {
            //     file:文件的数据
            // }
            let params = new FormData();
            params.append("file", f)
                // 2.发请求
            axios.post(url, params).then((res) => {
                console.log(res);
                let url = res.data.url;
                let img = document.createElement("img");
                img.src = url;
                document.body.appendChild(img);

            })
        })

    })
</script>

</html>